import { ref } from 'vue'
import { Notify } from "vant";
import { createFace } from '@/api/my/face.js'
import { Notify } from 'vant'

// 创建人脸钩子
function useCreateFace(isHaveFaceInfo, cancelBtn, getFaceInfo) {
  let isAgree = ref(false)

  function createFace() {
    if (!isAgree.value) {
      Notify({
        type: 'warning',
        duration: 1000,
        message: '你还没有勾选协议哦！',
      })
      return
    }
    const image = getImage(userNumber.value)
  
    loading.value = true

    let res

    // 判断是否录入人脸，有则更新人脸，没有则录入
    if (isHaveFaceInfo.value) {
      res = await updateFace(image)
    } else {
      res = await createFace(image)
    }
    

    if (res.data.code === 400) {
      Notify({ type: 'warning', duration: 1000, message: res.data.message })
      faceDetectionText.value = res.data.message
      loading.value = false
      return
    }

    loading.value = false
    await getFaceInfo(0)
    cancelBtn()

    if (res.data.data === "创建成功")
      Notify({ type: 'success', duration: 1000, message: '人脸录入成功！' })
    else if (res.data.data === "更新成功")
      Notify({ type: 'success', duration: 1000, message: '人脸更新成功！' })
  }

  return {
    isAgree
  }
}

export default goCreateFace;
